<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型:" label-width="180px" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择设备类型">
          <el-option
            v-for="dict in equipTypeOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门:" label-width="180px" prop="deptId">
        <el-tree-select
          v-model="queryParams.deptId"
          :data="deptOptions"
          :props="{ value: 'id', label: 'label', children: 'children' }"
          value-key="id"
          placeholder="请选择部门"
          check-strictly
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="设备类型" prop="typeName" :show-overflow-tooltip="true" />
      <el-table-column label="设备名称" prop="equipName" />
      <el-table-column label="所属部门" prop="deptName" />
      <el-table-column
        label="区域名称"
        prop="areaName"
        width="255"
        :show-overflow-tooltip="true"
      />

      <el-table-column label="设备坐标" prop="coords" />
      <el-table-column label="创建时间" prop="createdAt">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['risk:event:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['risk:event:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    <!--新增或者修改网络设备对话框-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item label="设备类型:" label-width="180px" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择设备类型">
            <el-option
              v-for="dict in equipTypeOption"
              :key="dict.id"
              :label="dict.label"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称:" label-width="180px" prop="equipName">
          <el-input v-model="form.equipName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="所在部门:" label-width="180px" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="deptOptions"
            :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id"
            placeholder="请选择部门"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="所在区域名称:" label-width="180px" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入设备所在区域名称或描述" />
        </el-form-item>
        <el-form-item label="三维地图坐标:" label-width="180px" prop="coords">
          <el-input v-model="form.coords" placeholder="请输入设备所在三维地图坐标" />
        </el-form-item>
        <el-form-item
          v-if="form.typeId == '3' && form.typeId != undefined"
          label="设备RTSP地址:"
          label-width="180px"
          prop="dataAddress"
        >
          <el-input v-model="form.dataAddress" placeholder="请输入摄像头的拉流地址" />
        </el-form-item>
        <el-form-item
          v-if="form.typeId == '3' && form.typeId != undefined"
          label="摄像头所在头层:"
          label-width="180px"
        >
          <el-input v-model="form.floor" placeholder="请输入摄像头所在楼层" />
        </el-form-item>
        <el-form-item
          v-if="form.typeId == '3' && form.typeId != undefined"
          label="摄像头所在位置经度:"
          label-width="180px"
        >
          <el-input v-model="form.longitude" placeholder="摄像头所在位置经度" />
        </el-form-item>
        <el-form-item
          v-if="form.typeId == '3' && form.typeId != undefined"
          label="摄像头所在位置纬度:"
          label-width="180px"
        >
          <el-input v-model="form.latitude" placeholder="摄像头所在位置纬度" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="ScreenVideo">
import {
  listVideo,
  addVideo,
  deleteVideo,
  updateVideo,
  getVideo,
} from "@/api/screen/video";
import { deptTreeSelect } from "@/api/system/user";
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const title = ref("");
const open = ref(false);
const { proxy } = getCurrentInstance();
const deptOptions = ref([]);
const equipTypeOption = ref([
  { id: 7, label: "消防炮" },
  { id: 6, label: "消防栓" },
  { id: 3, label: "摄像头" },
]);
const data = reactive({
  form: {
    typeId: undefined,
  },
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
  },
  rules: {
    equipName: [
      { required: true, message: "设备名称必须填写！", trigger: ["blur", "change"] },
      {
        min: 3,
        max: 50,
        message: "名称必须介于 3 和 50 之间",
        trigger: "blur",
      },
    ],
    typeId: [{ required: true, message: "设备类型必须选择！", trigger: "blur" }],
    deptId: [{ required: true, message: "部门必须选择！", trigger: "blur" }],
    areaName: [{ required: true, message: "所在区域请做简单描述！", trigger: "blur" }],
    coords: [{ required: true, message: "三维坐标必须填写！", trigger: "blur" }],
    dataAddress: [{ required: true, message: "请填写摄像头拉流地址！", trigger: "blur" }],
  },
});
const { queryParams, form, rules } = toRefs(data);
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

/** 获取网络设备数据 */
function getList() {
  loading.value = true;
  listVideo(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}
/** 重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}

/** 处理新增 */
function handleAdd() {
  open.value = true;
  title.value = "新增设备";
  reset();
}

/** 处理删除 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.equipName + '"的数据项?')
    .then(function () {
      return deleteVideo(row.equipId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 处理编辑 */
function handleUpdate(row) {
  getVideo(row.equipId).then((response) => {
    open.value = true;
    title.value = "编辑设备";
    form.value = response.data;
  });
}
/** 处理取消 */
function cancel() {
  open.value = false;
}
/** 数据验证 */
function volidateForm() {
  if (form.value.typeId == "" || form.value.typeId == undefined) {
    proxy.$modal.msgError("请选择设备类型");
    return;
  }
  if (form.value.equipName == "" || form.value.equipName == undefined) {
    proxy.$modal.msgError("设备名称必须填写");
    return;
  }
  if (form.value.deptId == "" || form.value.deptId == undefined) {
    proxy.$modal.msgError("所在属部门必须填写");
    return;
  }
  if (form.value.areaName == "" || form.value.areaName == undefined) {
    proxy.$modal.msgError("所在区域名称必须填写");
    return;
  }
  if (form.value.coords == "" || form.value.coords == undefined) {
    proxy.$modal.msgError("三位地图坐标必须填写!");
    return;
  }
  if (
    form.value.dataAddress == "" ||
    (form.value.dataAddress == undefined && form.value.typeId == "3")
  ) {
    proxy.$modal.msgError("摄像头的拉流地址必须填写");
    return;
  }
  if (form.value.typeId == "" || form.value.typeId == undefined) {
    proxy.$modal.msgError("请选择设备类型");
    return;
  }
  return true;
}
/** 处理数据提交 */
function submitForm() {
  if (volidateForm()) {
    if (form.value.equipId != undefined) {
      updateVideo(form.value).then((response) => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    } else {
      addVideo(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
getList();
getDeptTree();
</script>
